// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "main.js" as Engine

MyPage {
    id: settingsPage


    Component {
        id: cmpSounds
        Column {
            spacing: 10
            width: flickArea.width
            /*-------------------------------------------------*/
            Text {
                text: qsTr("Message received")
                font.pixelSize: 20
                font.bold: true
                anchors.horizontalCenter: parent.horizontalCenter
            }
            Item {
                id: itemIncommingMsg
                width: parent.width
                height: 64
                Text {
                    id: textIncommingMsg
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnFileIncommingMsg.left
                    anchors.rightMargin: 10
                    text: settings.soundIncomingMessage == "" ? "Empty" : Engine.baseName( settings.soundIncomingMessage )
                    color: settings.soundIncomingMessage == "" ? "gray" : "black"
                    //font.italic: settings.soundIncomingMessage == "" ? true : false
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyMessageRecv()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textIncommingMsg.font.bold = true
                            } else {
                                textIncommingMsg.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnFileIncommingMsg
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearIncommingMsg.left
                    anchors.rightMargin: 10
                    height: parent.height - 4
                    width: 72
                    onClicked: {
                        //chooseFilesSoundInMsg.dlgShowHide()
                        var p = Engine.basePath( settings.soundIncomingMessage )
                        //console.log( "presetPath:" + p )
                        Engine.dlgFilesChoose( settingsPage, p, funcFileSoundInMsg )
                    }
                }
                ToolButton {
                    id: btnClearIncommingMsg
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundIncomingMessage = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemIncommingMsg ... }

            /*-------------------------------------------------------------------*/

            Text {
                text: qsTr("Message sent")
                font.pixelSize: 20
                font.bold: true
                anchors.horizontalCenter: parent.horizontalCenter
            }
            Item {
                id: itemOutcommingMsg
                width: parent.width
                height: 64
                Text {
                    id: textOutcommingMsg
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnFileOutcommingMsg.left
                    anchors.rightMargin: 10
                    text: settings.soundOutcomingMessage == "" ? "Empty" : Engine.baseName( settings.soundOutcomingMessage )
                    color: settings.soundOutcomingMessage == "" ? "gray" : "black"
                    //font.italic: settings.soundOutcomingMessage == "" ? true : false
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyMessageSent()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textOutcommingMsg.font.bold = true
                            } else {
                                textOutcommingMsg.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnFileOutcommingMsg
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearOutcommingMsg.left
                    anchors.rightMargin: 10
                    height: parent.height-4
                    width: 72
                    onClicked: {
                        //chooseFilesSoundOutMsg.dlgShowHide()
                        var p = Engine.basePath( settings.soundOutcomingMessage )
                        Engine.dlgFilesChoose( settingsPage, p, funcFilesSoundOutMsg )
                    }
                }
                ToolButton {
                    id: btnClearOutcommingMsg
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundOutcomingMessage = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemIncommingMsg ... }

            /*----------------------------------------------------------------*/

            Text {
                text: qsTr("Buddy logs in")
                font.pixelSize: 20
                anchors.horizontalCenter: parent.horizontalCenter
                font.bold: true
            }
            Item {
                id: itemBuddyLogsIn
                width: parent.width
                height: 64
                Text {
                    id: textBuddyLogsIn
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnBuddyLogsIn.left
                    anchors.rightMargin: 10
                    text: settings.soundBuddyLogsIn == "" ? "Empty" : Engine.baseName( settings.soundBuddyLogsIn )
                    color: settings.soundBuddyLogsIn == "" ? "gray" : "black"
                    //font.italic: settings.soundBuddyLogsIn == "" ? true : false
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyBuddyLogsIn()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textBuddyLogsIn.font.bold = true
                            } else {
                                textBuddyLogsIn.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnBuddyLogsIn
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearBuddyLogsIn.left
                    anchors.rightMargin: 10
                    height: parent.height-4
                    width: 72
                    onClicked: {
                        //chooseFilesSoundIBuddyLogsIn.dlgShowHide()
                        var p = Engine.basePath( settings.soundBuddyLogsIn )
                        Engine.dlgFilesChoose( settingsPage, p, funcFilesSoundIBuddyLogsIn )
                    }
                }
                ToolButton {
                    id: btnClearBuddyLogsIn
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundBuddyLogsIn = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemBuddyLogsIn ... }

            /*---------------------------------------------------------------------*/
            Text {
                text: qsTr("Buddy logs Out")
                font.pixelSize: 20
                anchors.horizontalCenter: parent.horizontalCenter
                font.bold: true
            }
            Item {
                id: itemBuddyLogsOut
                width: parent.width
                height: 64
                Text {
                    id: textBuddyLogsOut
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnBuddyLogsOut.left
                    anchors.rightMargin: 10
                    text: settings.soundBuddyLogsOut == "" ? "Empty" : Engine.baseName( settings.soundBuddyLogsOut )
                    color: settings.soundBuddyLogsOut == "" ? "gray" : "black"
                    //font.italic: settings.soundBuddyLogsOut == "" ? true : false
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyBuddyLogsOut()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textBuddyLogsOut.font.bold = true
                            } else {
                                textBuddyLogsOut.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnBuddyLogsOut
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearBuddyLogsOut.left
                    anchors.rightMargin: 10
                    height: parent.height-4
                    width: 72
                    onClicked: {
                        //chooseFilesSoundIBuddyLogsOut.dlgShowHide()
                        var p = Engine.basePath( settings.soundBuddyLogsOut )
                        Engine.dlgFilesChoose( settingsPage, p, funcFilesSoundIBuddyLogsOut )
                    }
                }
                ToolButton {
                    id: btnClearBuddyLogsOut
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundBuddyLogsOut = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemBuddyLogsIn ... }

            /*-------------------------------------------------*/
            Text {
                text: qsTr("Someone says your name in chat")
                font.pixelSize: 20
                font.bold: true
                anchors.horizontalCenter: parent.horizontalCenter
            }
            Item {
                id: itemSaysYourName
                width: parent.width
                height: 64
                Text {
                    id: textSaysYourName
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnFileSaysYourName.left
                    anchors.rightMargin: 10
                    text: settings.soundChatSomeoneSaysMyName == "" ? "Empty" : Engine.baseName( settings.soundChatSomeoneSaysMyName )
                    color: settings.soundChatSomeoneSaysMyName == "" ? "gray" : "black"
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyChatSomeoneSaysMyName()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textSaysYourName.font.bold = true
                            } else {
                                textSaysYourName.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnFileSaysYourName
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearSaysYourName.left
                    anchors.rightMargin: 10
                    height: parent.height - 4
                    width: 72
                    onClicked: {
                        var p = Engine.basePath( settings.soundChatSomeoneSaysMyName )
                        Engine.dlgFilesChoose( settingsPage, p, funcFileSaysYourName )
                    }
                }
                ToolButton {
                    id: btnClearSaysYourName
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundChatSomeoneSaysMyName = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemSaysYourName ... }

            /*-------------------------------------------------*/
            Text {
                text: qsTr("Others talk")
                font.pixelSize: 20
                font.bold: true
                anchors.horizontalCenter: parent.horizontalCenter
            }
            Item {
                id: itemOthersSay
                width: parent.width
                height: 64
                Text {
                    id: textOthersSay
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnFileOthersSay.left
                    anchors.rightMargin: 10
                    text: settings.soundChatOthersTalk == "" ? "Empty" : Engine.baseName( settings.soundChatOthersTalk )
                    color: settings.soundChatOthersTalk == "" ? "gray" : "black"
                    font.italic: true
                    font.pixelSize: 22
                    clip: true
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            notify.notifyChatOthersTalk()
                        }
                        onPressedChanged: {
                            if( pressed ) {
                                textOthersSay.font.bold = true
                            } else {
                                textOthersSay.font.bold = false
                            }
                        }
                    } //MouseArea
                }
                Button {
                    id: btnFileOthersSay
                    text: "..."
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: btnClearOthersSay.left
                    anchors.rightMargin: 10
                    height: parent.height - 4
                    width: 72
                    onClicked: {
                        var p = Engine.basePath( settings.soundChatOthersTalk )
                        Engine.dlgFilesChoose( settingsPage, p, funcFileChatOthersTalk )
                    }
                }
                ToolButton {
                    id: btnClearOthersSay
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    icon: "qrc:/qml/images/bar_close.png"
                    onClicked: {
                        settings.soundChatOthersTalk = ""
                    }
                    pauseAnim: 1
                }
            } // Item{ id: itemOthersSay ... }
        } //Column
    }


    Component {
        id: cmpVibration
        Column {
            spacing: 10
            width: flickArea.width
            MyCheckBox {
                id: vibrMsgRecv
                text: qsTr("Message received")
                font.pixelSize: 24
                checked: settings.vibrationMessageReceived
                onCheckedChanged: {
                    settings.vibrationMessageReceived = checked
                }
            }
            MyCheckBox {
                id: vibrMesgSent
                text: qsTr("Message sent")
                font.pixelSize: 24
                checked: settings.vibrationMessageSent
                onCheckedChanged: {
                    settings.vibrationMessageSent = checked
                }
            }
            MyCheckBox {
                id: vibrChatSaysYourName
                text: qsTr("Someone says you name")
                font.pixelSize: 24
                checked: settings.vibrationChatSomeoneSaysMyName
                onCheckedChanged: {
                    settings.vibrationChatSomeoneSaysMyName = checked
                }
            }
            MyCheckBox {
                id: vibrChatOthersTalk
                text: qsTr("Others talk")
                font.pixelSize: 24
                checked: settings.vibrationChatOthersTalk
                onCheckedChanged: {
                    settings.vibrationChatOthersTalk = checked
                }
            }
        }
    }


    Component {
        id: cmpNotification
        Column {
            spacing: 10
            width: flickArea.width
            MyCheckBox {
                id: notifyConnecting
                text: qsTr("Connecting")
                font.pixelSize: 24
                checked: settings.notifyConnection
                onCheckedChanged: {
                    settings.notifyConnection = checked
                }
            }
            MyCheckBox {
                id: notifyOnlibe
                text: qsTr("Online")
                font.pixelSize: 24
                checked: settings.notifyOnline
                onCheckedChanged: {
                    settings.notifyOnline = checked
                }
            }
            MyCheckBox {
                id: notifyOffline
                text: qsTr("Offline")
                font.pixelSize: 24
                checked: settings.notifyOffline
                onCheckedChanged: {
                    settings.notifyOffline = checked
                }
            }
            MyCheckBox {
                id: notifyMsgReceived
                text: qsTr("Message received")
                font.pixelSize: 24
                checked: settings.notifyMessage
                onCheckedChanged: {
                    settings.notifyMessage = checked
                }
            }
            MyCheckBox {
                id: notifyBoxTyping
                text: qsTr("Typing")
                font.pixelSize: 24
                checked: settings.notifyTyping
                onCheckedChanged: {
                    settings.notifyTyping = checked
                }
            }
        }
    }


    Component {
        id: cmpConnection
        Column {
            spacing: 10
            width: flickArea.width
            MyCheckBox {
                id: cbNeedReconnect
                text: qsTr("Reconnect when error")
                font.pixelSize: 24
                checked: settings.reconnectWhenError === true ? true : false
                onCheckedChanged: {
                    console.log("Reconnect when error: checked="+checked)
                    xmppClient.reconnectOnError = checked
                    settings.reconnectWhenError = checked
                }
            }
            Text {
                text: qsTr("Keep alive interval (secs)")
                font.pixelSize: 20
                anchors.horizontalCenter: parent.horizontalCenter
                font.bold: true
            }
            MyTextInput {
                id: tiKeepAlive
                anchors.horizontalCenter: parent.horizontalCenter
                height: 50
                width: parent.width - 20
                Component.onCompleted: {
                    tiKeepAlive.text = settings.keepAliveInterval
                }
                onTextChanged: {
                    var interval = parseInt(tiKeepAlive.text)
                    xmppClient.keepAlive = interval
                    settings.keepAliveInterval = interval
                }
            }
            Item {
                id: space
                anchors.horizontalCenter: parent.horizontalCenter
                width: parent.width
                height: 50
            }
        }
    }


    Component {
        id: cmpSmilesTheme
        Column {
            spacing: 10
            width: flickArea.width
            Text {
                id: textCurrentTheme
                font.pixelSize: 24
                anchors.horizontalCenter: parent.horizontalCenter
            }
            ListView {
                id: listModelThemeSmiles
                delegate: delegateThemeSmiles
                model: mainSmiles.themeSmiles
                height: 400
                width: parent.width
                clip: true
            }
        }
    }


    Component {
        id: delegateThemeSmiles
        Rectangle {
            id: wrapper
            width: listModelThemeSmiles.width-4
            height: 70
            border.color: "gray"
            anchors.horizontalCenter: parent.horizontalCenter
            Image {
                id: imgTheme
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 15
                smooth: true
                height: 58; width: height
                source:smileThemePicture
            }
            Text {
                id: textName
                font.pixelSize: 28
                anchors.left: imgTheme.right
                anchors.leftMargin: 15
                anchors.top: parent.top
                anchors.topMargin: 5
                text: smileThemeName
            }
            Text {
                id: textAuthor
                font.pixelSize: 16
                color: "gray"
                anchors.left: imgTheme.right
                anchors.leftMargin: 15
                anchors.top: textName.bottom
                anchors.topMargin: 5
                text: qsTr("Author") + ": " + smileThemeAuthor
            }
            MouseArea {
                id: mouseAreaItem;
                anchors.fill: parent
                onClicked:
                {
                    wrapper.ListView.view.currentIndex = index
                    settings.smilesTheme = smileThemeFile
                    mainSmiles.file = smileThemeFile
                    textCurrentTheme.text = qsTr("Current theme is") + " <b>'" + smileThemeName + "'</b>"
                }
            }
            Component.onCompleted: {
                console.log("mainSmiles.file="+mainSmiles.file)
                console.log("index="+index)
                if (settings.smilesTheme == smileThemeFile) {
                    textCurrentTheme.text = qsTr("Current theme is") + " <b>'" + smileThemeName + "'</b>"
                }
            }
            states: State {
                name: "Current"
                when: (wrapper.ListView.isCurrentItem && (mainSmiles.file = smileThemeFile) )
                PropertyChanges { target: wrapper; color: "lightblue" }
            }
            //color: (settings.smilesTheme == smileThemeFile) ? "lightblue" : "white"
        }
    }

    Flickable {
        id: flickArea
        anchors.top: parent.top; anchors.topMargin: 5
        anchors.bottom: toolBar.top; anchors.bottomMargin: 5
        anchors.left: parent.left; anchors.leftMargin: 10
        anchors.right: parent.right; anchors.rightMargin: 10

        contentHeight: columnContent.height
        contentWidth: columnContent.width

        flickableDirection: Flickable.VerticalFlick

        Column {
            id: columnContent
            width: settingsPage.width - flickArea.anchors.rightMargin - flickArea.anchors.leftMargin
            spacing: 5

            HiddenBlock {
                id: soundNotify
                state: "open"
                contentBlock: cmpSounds
                title: qsTr("Sounds")
            }
            HiddenBlock {
                id: vibrNotify
                state: "close"
                contentBlock: cmpVibration
                title: qsTr("Vibration")
            }
            HiddenBlock {
                id: optNotify
                state: "close"
                contentBlock: cmpNotification
                title: qsTr("Notification")
            }
            HiddenBlock {
                id: optConnection
                state: "close"
                contentBlock: cmpConnection
                title: qsTr("Connection")
            }
            HiddenBlock {
                id: optThemeSmiles
                state: "close"
                contentBlock: cmpSmilesTheme
                title: qsTr("Smiles")
            }
        }
    }

    /****************************************************/

    function funcFileSoundInMsg( fileSelected ) {
        settings.soundIncomingMessage = fileSelected
    }

    function funcFilesSoundOutMsg( fileSelected ) {
        settings.soundOutcomingMessage = fileSelected
    }

    function funcFilesSoundIBuddyLogsIn( fileSelected ) {
        settings.soundBuddyLogsIn = fileSelected
    }

    function funcFilesSoundIBuddyLogsOut( fileSelected ) {
        settings.soundBuddyLogsOut = fileSelected
    }

    function funcFileSaysYourName( fileSelected ) {
        settings.soundChatSomeoneSaysMyName = fileSelected
    }

    function funcFileChatPersonEnters( fileSelected ) {
        settings.soundChatPersonEnters = fileSelected
    }

    function funcFileChatPersonLeaves( fileSelected ) {
        settings.soundChatPersonLeaves = fileSelected
    }

    function funcFileChatITalk( fileSelected ) {
        settings.soundChatChatITalk = fileSelected
    }

    function funcFileChatOthersTalk( fileSelected ) {
        settings.soundChatOthersTalk = fileSelected
    }

    /****************************************************/
    ToolBar {
        id: toolBar
        ToolButton {
            id: toolBarButtonDialog
            icon: "qrc:/qml/images/bar_roster.png"
            anchors.left: parent.left
            anchors.leftMargin: 30
            onClicked: {
                settingsPage.closePage( "qrc:/qml/RosterPage.qml" )
            }
            pauseAnim: 500
        }
    } //ToolBar

} //MyPage
